classdef FekoFile_Phi < handle
	
	properties
		nl_;
		fileExt_;
		fileName_;
		freqStr_;
		entryCount_;
		entryStr_;
		fileStr_;
		fileStartStr_;
	end
	
	methods
		
		function obj = FekoFile_Phi( fileName, freq )
			
			obj.nl_ = string( newline );
			obj.fileExt_ = ".efe";
			obj.fileName_ = fileName;
			obj.freqStr_ = string( sprintf( '%1.14E', freq ) );
			obj.entryCount_ = 0;
			obj.entryStr_ = "";
			obj.fileStr_ = "";
			obj.fileStartStr_ = ...
				"##File Type: Electric near field" + obj.nl_ + ...
				"##File Format: 3" + obj.nl_ + ...
				"##Source: tet1" + obj.nl_ + ...
				"##Date: 2018-09-10 12:34:56" + obj.nl_ + ...
				"** File exported by POSTFEKO version 7.0.1-256428 (x64)" + obj.nl_ + ...
				obj.nl_ ...
			;
		
		end
		
		function GenerateFileStr( obj )
			
			obj.fileStr_ = obj.fileStartStr_ + ...
				"#Request Name: " + obj.fileName_ + obj.nl_ + ...
				"#Frequency: " + obj.freqStr_ + obj.nl_ + ...
				"#Coordinate System: Cylindrical" + obj.nl_ + ...
				"#No. of Rho Samples: 1" + obj.nl_ + ...
				"#No. of Phi Samples: " + string( sprintf( '%i', obj.entryCount_ ) ) + obj.nl_ + ...
				"#No. of Z Samples: 1" + obj.nl_ + ...
				"#Result Type: Electric Field Values" + obj.nl_ + ...
				"#No. of Header Lines: 1" + obj.nl_ + ...
				string( '#                 "Rho"             "Phi"             "Z"               "Re(Erho)"        "Im(Erho)"        "Re(Ephi)"        "Im(Ephi)"        "Re(Ez)"          "Im(Ez)"          ' ) + obj.nl_ + ...
				obj.entryStr_ + obj.nl_ ...
			;
		
		end
		
		function InsertEntry( obj, r, p, z, Err, Eri, Epr, Epi, Ezr, Ezi )
			
			obj.entryStr_ = obj.entryStr_ + ...
				"                   " + string( sprintf( '%1.14E', r ) ) + ...
				"    " + string( sprintf( '%1.14E', p ) ) + ...
				"    " + string( sprintf( '%1.14E', z ) ) + ...
				"    " + string( sprintf( '%1.14E', Err ) ) + ...
				"    " + string( sprintf( '%1.14E', Eri ) ) + ...
				"    " + string( sprintf( '%1.14E', Epr ) ) + ...
				"    " + string( sprintf( '%1.14E', Epi ) ) + ...
				"    " + string( sprintf( '%1.14E', Ezr ) ) + ...
				"    " + string( sprintf( '%1.14E', Ezi ) ) + ...
				"   " + obj.nl_ ...
			;
			obj.entryCount_ = obj.entryCount_ + 1;
			
		end
		
		function SaveFile( obj )
			
			obj.GenerateFileStr();
			fileId = fopen( char( obj.fileName_ + obj.fileExt_ ), 'w' );
			fprintf( fileId, '%s', obj.fileStr_ );
			fclose( fileId );
			
		end
		
	end
	
end

